<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>产品开发申请 - 产品数据管理系统</title>
{include file='top.tpl'}
</head>
<body class="skin-blue">
<div class="wrapper">
	{include file='bread_crumbs.tpl'}
	<!--引入面包屑-->
	<section class="base-setting-wrap">
		<div class="row">
			<div class="col-md-12">
				<div class="box">
					<form id="js_form">
		                <div class="box-body">		
		                  	<table class="table table-bordered table-striped table-hover" id="js_productTable">
		                    	<tbody>
				                    <tr>
				                    	<td class="text-right" width="120"><b>请输入产品编码：<em class="text-star"></em></b><p>（多个用英文逗号隔开）</p></td>
				                    	<td class="text-left">
				                    		<textarea name="sku" class="form-control" rows="3" cols=""></textarea>
				                    		<input type="hidden" name='valid_sku' value="" >
				                    	</td>
									</tr>
									<tr>
				                    	<td class="text-right"><b>产品名称(自动产生)：<em class="text-star"></em></b></td>
				                    	<td class="text-left">
				                    		<p class="form-control-static" id="product-names"></p>		                    		
				                    	</td>
									</tr>
									<tr>
				                    	<td class="text-right"><b>需求数量：<em class="text-star"></em></b></td>
				                    	<td class="text-left">
				                    		<input type="text" name="num" class="form-control"><span class="help-block">样衣数量不能大于3</span>	                    		
				                    	</td>
									</tr>
									<tr>
				                    	<td class="text-right"><b>备注：<em class="text-star"></em></b></td>
				                    	<td class="text-left">
				                    		<textarea class="form-control" name="remark" rows="3" cols=""></textarea>
				                    	</td>
									</tr>
									<tr>
				                    	<td class="text-right"><b>是否生成同款产品样衣需求：<em class="text-star"></em></b></td>
				                    	<td class="text-left">
				                    		<label class="mr10"><input type="radio" name="process_same_sku" value='1' checked='checked'>是</label>
				                    		<label><input type="radio" name="process_same_sku" value='0'>否</label>
				                    	</td>
									</tr>
									<tr>
				                    	<td class="text-right"><b>样衣类型：<em class="text-star">&nbsp;&nbsp;</em></b></td>
				                    	<td class="text-left">
				                    		{foreach $type as $k => $t}
			                				<label class="mr10">
			                					<input type="radio" name="sample_type" value="{$k}" {if $k==1}checked='checked'{/if}>{$t}
			                				</label> 
			                				{/foreach}
				                    	</td>
									</tr>
								</tbody>					                 
		              		</table>
		                </div>
	                </f orm>
	                <!-- .box-body -->
	                <div class="box-footer text-center">
						<button type="button" class="btn btn-primary btn-sm" id="js_submitBtn">提交</button>
						<a href="javascript:history.back();" class="btn btn-default btn-sm">返回</a>
	                </div>
	            </div>
			</div>
		</div>
	</section>
</div>

{include file='foot.tpl'}
<script>
{literal}
$(function(){
	$("#js_submitBtn").bind("click", function(){
		var sku = $("textarea[name=sku]");
		var valid = $("input[name=valid_sku]");
		var num = $("input[name=num]");

		if (!sku.val().trim()) {
			layer.alert("sku不能为空");
			return false;
		} else if(!valid.val()) {
			layer.alert("没有填写有效的sku，无法提交");
			return false;
		}
		if (!num.val().trim()) {
			layer.alert("需求数量不能为空");
			return false;
		} else if(isNaN(num.val().trim())) {
			layer.alert("需求数量只能是数字");
			return false;
		} else if(parseInt(num.val().trim()) > 3) {
			layer.alert("样衣数量不能大于3");
			return false;
		}
		var url = "/purchase/sample-cloth/add-deal";
		var data = $("#js_form").serialize();
		
		$.post(url, data, function(data) {
			if (data.status == 1) {
				layer.alert(data.msg);
			}else {
				layer.alert(data.message);
			}
		}, 'json');
	})
	// 保证不重复提交相同数据
	var last = '';
	// 获取产品名称
	$("textarea[name=sku]").bind("blur", function(){
		if (skus = $(this).val().trim()) {
			if (skus == last) {
				return false;
			};
			$.ajax({
        		type: "GET",
        		cache:false,
        		dataType:'json',
        		url: "/purchase/sample-cloth/get-name?skus="+skus, 
        		success: function(data){
        			var goodnames = []; 
        			var valid = [];
        			var un_valid = [];
        			for(good in data) {
        				if (typeof(data[good]) == 'object') {
        					valid.push(good);
        					goodnames.push(data[good].product_name);
						} else {
							un_valid.push(good);
						}
        			}
        			$("#product-names").text(goodnames.join());
        			if (un_valid.length) {
        				layer.alert("以下产品编码不存在：<br/>" + un_valid.join() + "<br/>请检查");
        			};
        			$("input[name=valid_sku]").val(valid.join());
        			last = skus;
        		}
        	});
		};
	})
})
{/literal}
</script>
</body>
</html>
